//
//  UserService.swift
//  chat
//
//  Created by 崔世强 on 2018/11/7.
//  Copyright © 2018 崔世强. All rights reserved.
//

import Foundation
import SwiftyJSON


class UserService {
    
    func register(userName:String,pwd:String,validateCode:String,callBack: @escaping (_ success:Bool,_ msg:String?,_ userId:Int?) -> ()) {
//        imApi.request(.register(userName:userName,pwd:pwd,validateCode:validateCode)) { (result) in
//            if case let .success(response) = result {
//                let json = JSON(response.data)
//                if json["code"].stringValue == "10000" {
//                     callBack(true,nil,json["res"].intValue)
//                } else {
//                    callBack(false,json["msg"].stringValue,nil)
//                }
//            } else {
//                callBack(false,"注册失败",nil)
//            }
//        }
        IM.request(.register(userName:userName,pwd:pwd,validateCode:validateCode),success: { (json) in
            if reqSuccess(json["code"].stringValue) {
                callBack(true,nil,json["res"].intValue)
            } else {
                let msg = json["msg"].stringValue
                print(msg)
                callBack(false,msg,nil)
            }
        }, error: { (code) in
            print("状态异常：\(code)")
            callBack(false,"注册失败",nil)
        }) { (error) in
            print(error)
            callBack(false,"注册失败",nil)
        }
        
    }
    
    func login(_ uid: Int, _ nickName: String,avatar:String?=nil,gender:String? = nil ) {
        let Defaults = UserDefaults(suiteName: USER_DEFAULTS_NAME)!
        Defaults[.userId] = uid
        Defaults[.username] = nickName
        Defaults[.userAvatar] = avatar
        Defaults[.userGender] = gender
        AppStatusManager.instance.userLoginStatus = (true,uid,nickName)
        AppStatusManager.instance.avatarPath = avatar
        ConnectionManager.instance.connect()
    }
    
    func updateMyInfo(_ uid: Int, name:String? = nil,avatar:String? = nil,gender:Int? = nil,callBack: @escaping (_ success:Bool,_ msg:String?) -> ()) {
        IM.request(.modifyInfo(userId: uid, name: name, avatar: avatar, gender: gender),success: { (json) in
            if reqSuccess(json["code"].stringValue) {
                let Defaults = UserDefaults(suiteName: USER_DEFAULTS_NAME)!
                if let name = name {
                    Defaults[.username] = name
                }
                if let avatar = avatar {
                    Defaults[.userAvatar] = avatar
                }
                if let gender = gender {
                    Defaults[.userGender] = String(gender)
                }
                callBack(true,nil)
            } else {
                let msg = json["msg"].stringValue
                print(msg)
                callBack(false,msg)
            }
        }, error: { (code) in
            print("状态异常：\(code)")
            callBack(false,"设定失败")
        }) { (error) in
            print(error)
            callBack(false,"设定失败")
        }
    }
}
